<?php

/**
* 资金管理
*
* @package		Hooloo framework
* @author 		Bill
* @copyright 	Hooloo Co.,Ltd
* @version		1.0
* @release		2017.05.25
*/
defined('BASEPATH') OR exit('No direct script access allowed');

class Atm  extends Controller{
	public function __construct(){
		parent::__construct();
	}
	
	/**
     * 资金转出
     */
	public function index(){
		//获取用户资金情况
		$uid = $_SESSION["user"]["id"];

		//查询用户信息
		$sql = "select id, username, sex, nickname, realname, mobile, is_locked, is_stop, is_disable, try_times, last_try, password, atmpwd, demo_account, aid, bank_name, bank_code, bank_account, bank_address from user where id = $uid";
		$user = $this->db->query($sql)->row_array();

		$atm = get_user($uid, 1);

		if(IS_POST){
			//试玩帐号不允许提现
			if ($user["demo_account"] == 1) {
				ajax_return(0, "试玩帐号不允许提现");
			}
			if ($user["is_disable"] == 1) {
				ajax_return(0, "帐号已禁用，禁止资金转出");
			}
			if ($user["is_locked"] == 1) {
				ajax_return(0, "帐号已锁定，禁止资金转出");
			}
			//接收参数
			$money = isset($_POST["money"]) ? round($_POST["money"], 2) : 0;
			$atmpwd = isset($_POST["atmpwd"]) ? $_POST["atmpwd"] : "";
			
			//校验参数
			if (0 >= $money) {
				ajax_return(0, "请输入转账金额");
			}
			if (! $atmpwd) {
				ajax_return(0, "请输入取款密码");
			} elseif (md5($atmpwd) != $user["atmpwd"]) {
				//ajax_return(0, "取款密码不正确");
			}
			
			//判断资金是否充足
			if ($money > $atm["cash"]) {
				ajax_return(0, "您最多可以转出" . $atm["cash"] . "元");
			}
			
			//设置参数
			$aid = $user["aid"];
			$bank_name = $user["bank_name"];
			$bank_account = $user["bank_account"];
			$bank_no = $user["bank_code"];
			$bank_address = $user["bank_address"];
			
			//事务开启
			$this->db->trans_begin();
			
			//转账申请
			$sql = "insert into log_atm (uid, aid, money, bank_name, bank_account, bank_no, bank_address) values ($uid, $aid, $money, '$bank_name', '$bank_account', '$bank_no', '$bank_address')";
			$this->db->query($sql);
			if (! $this->db->insert_id()) {
				//事务回滚
				$this->db->trans_rollback();
				
				ajax_return(0, "申请提款失败");
			}

			//发送提款信息
			$title = "用户【" . $user["username"] . "】申请提款" . $money . "元!";
			$content = "申请提款信息如下：用户" . $user["username"] . "申请提款提款金额：" . $money . "元";
			$sql = "insert into message (aid, sender, receiver, title, content, isread) values ({$aid}, {$uid}, 0, '{$title}', '{$content}', 0)";
			$this->db->query($sql);
			if (! $this->db->insert_id()) {
				//事务回滚
				$this->db->trans_rollback();
				
				ajax_return(0, "申请提款失败");
			}
			
			//冻结额度
			$res = common_user($uid, -$money, -($money * 10), 0, "申请提现{$money}元");
			if (! $res) {
				//事务回滚
				$this->db->trans_rollback();
				
				ajax_return(0, "申请提款失败");
			}
			
			//提交事务
			$this->db->trans_commit();
			
			//返回结果
			ajax_return(1, "转账已提交，等待管理员审核。", "/atm/cash");
		}

		$this->assign("user", $user);
		$this->assign("atm", $atm);
		$this->display();
	}
	
	/**
     * 提现记录
     */
	public function cash($start_time = 0, $end_time = 0, $p = 1) {
		//通用搜索
		$uid = $_SESSION["user"]["id"];
		$where = " where uid = $uid";
		
		//搜索时间
		if (! $start_time) $start_time = date("Y-m-d"); 
		if (! $end_time) $end_time = date("Y-m-d");
		if ($start_time > $end_time) $this->error("开始时间不能大于结束时间！");
		$where .= " and addtime >= '" . $start_time . "' and addtime <= '" . ($end_time . " 23:59:59") . "'";
		
		//数据分页
		$p = (int)$p <= 0 ? 1 : (int)$p;
		$pize = 15;
		$start = ($p - 1) * $pize;
		
		//统计总数
		$sql = "select count(*) as c from log_atm " . $where;
		$total = $this->db->query($sql)->row_array();
		$total = $total["c"];

		//分页查询
		$sql = "select * from log_atm " . $where . " order by id DESC limit $start, $pize";
		$list = $this->db->query($sql)->result_array();
		
		//分页展示
		$url = "/" . $this->_controller . "/" . $this->_action . $start_time . "/" . $end_time . "/";
		$page = page_format($total, $pize, $p, $url);
		
		//格式化数据
		foreach ($list as $key => $val) {
			$list[$key]["money"] = number_format($val["money"], 2);
		}
		
		//分配展示
		$this->assign("list", $list);
		$this->assign("page", $page);
		$this->assign("total", $total);
		$this->assign("start_time", $start_time);
		$this->assign("end_time", $end_time);
			
		IS_AJAX && ajax_return(1, "查询成功", $list);
		$this->display();
	}
	
	/**
     * 充值记录
     */
	public function charge($start_time = 0, $end_time = 0, $p = 1) {
		//通用搜索
		$uid = $_SESSION["user"]["id"];
		$where = " where uid = $uid";
		
		//搜索时间
		if (! $start_time) $start_time = date("Y-m-d"); 
		if (! $end_time) $end_time = date("Y-m-d");
		if ($start_time > $end_time) $this->error("开始时间不能大于结束时间！");
		$where .= " and addtime >= '" . $start_time . "' and addtime <= '" . ($end_time . " 23:59:59") . "'";
		
		//数据分页
		$p = (int)$p <= 0 ? 1 : (int)$p;
		$pize = 15;
		$start = ($p - 1) * $pize;
		
		//统计总数
		$sql = "select count(*) as c from log_charge " . $where;
		$total = $this->db->query($sql)->row_array();
		$total = $total["c"];

		//分页查询
		$sql = "select * from log_charge " . $where . " order by id DESC limit $start, $pize";
		$list = $this->db->query($sql)->result_array();
		
		//分页展示
		$url = "/" . $this->_controller . "/" . $this->_action . $start_time . "/" . $end_time . "/";
		$page = page_format($total, $pize, $p, $url);
		
		//格式化数据
		foreach ($list as $key => $val) {
			$list[$key]["money"] = number_format($val["money"], 2);
		}
		
		//分配展示
		$this->assign("list", $list);
		$this->assign("page", $page);
		$this->assign("total", $total);
		$this->assign("start_time", $start_time);
		$this->assign("end_time", $end_time);
			
		IS_AJAX && ajax_return(1, "查询成功", $list);
		$this->display();
	}
	
	/**
     * 资金流水
     */
	public function bill($start_time = 0, $end_time = 0, $p = 1) {
		//查询条件
		$uid = $_SESSION["user"]["id"];
		$where = " where uid = $uid and status = 0";
		
		//搜索时间
		if (! $start_time) $start_time = date("Y-m-d"); 
		if (! $end_time) $end_time = date("Y-m-d");
		if ($start_time > $end_time) $this->error("开始时间不能大于结束时间！");
		$where .= " and addtime >= '" . $start_time . "' and addtime <= '" . ($end_time . " 23:59:59") . "'";
		
		//查询总数
		$sql = "select count(*) c from log_bill " . $where;
		$total = $this->db->query($sql)->row_array();
		$total = $total["c"];
		
		//数据分页
		$pize = 15;//每页数量
		if(0 >= $p){
			$p = 1;
		}
		$start = ($p - 1) * $pize;//分页开始

		//查询记录
		$sql = "select * from log_bill " . $where . " order by id desc limit $start, $pize";
		$list = $this->db->query($sql)->result_array();

		//分页展示
		$url = "/" . $this->_controller . "/" . $this->_action . "/" . $start_time . "/". $end_time . "/";
		$page = page_format($total, $pize, $p, $url);
		
		$this->assign("list", $list);
		$this->assign("total", $total);
		$this->assign("page", $page);
		$this->assign("start_time", $start_time);
		$this->assign("end_time", $end_time);
		$this->display();
	}
}
